package com.bambooshoot.service;

import com.bambooshoot.common.core.page.PageQuery;
import com.bambooshoot.common.core.page.TableDataInfo;
import com.bambooshoot.common.utils.MapstructUtils;
import com.bambooshoot.common.utils.StringUtils;
import com.bambooshoot.domain.BizLiquorRecords;
import com.bambooshoot.domain.bo.BizLiquorRecordsBo;
import com.bambooshoot.domain.vo.BizLiquorRecordsVo;
import com.bambooshoot.mapper.BizLiquorRecordsMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 白酒记录Service业务层处理
 *
 * @author mxc
 * @date 2024-11-23
 */
@RequiredArgsConstructor
@Service
public class BizLiquorRecordsService extends ServiceImpl<BizLiquorRecordsMapper, BizLiquorRecords> {

    /**
     * 查询白酒记录
     *
     * @param id 主键
     * @return 白酒记录
     */
    public BizLiquorRecordsVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询白酒记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 白酒记录分页列表
     */
    public TableDataInfo<BizLiquorRecordsVo> queryPageList(BizLiquorRecordsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BizLiquorRecords> lqw = buildQueryWrapper(bo);
        Page<BizLiquorRecordsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的白酒记录列表
     *
     * @param bo 查询条件
     * @return 白酒记录列表
     */
    public List<BizLiquorRecordsVo> queryList(BizLiquorRecordsBo bo) {
        LambdaQueryWrapper<BizLiquorRecords> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BizLiquorRecords> buildQueryWrapper(BizLiquorRecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BizLiquorRecords> lqw = Wrappers.lambdaQuery();
        lqw.between(params.get("beginPurchaseDate") != null && params.get("endPurchaseDate") != null,
            BizLiquorRecords::getPurchaseDate, params.get("beginPurchaseDate"), params.get("endPurchaseDate"));
        lqw.eq(bo.getBrand() != null, BizLiquorRecords::getBrand, bo.getBrand());
        lqw.like(StringUtils.isNotBlank(bo.getShortName()), BizLiquorRecords::getShortName, bo.getShortName());
        lqw.eq(bo.getPlatform() != null, BizLiquorRecords::getPlatform, bo.getPlatform());
        lqw.ge(bo.getMinPrice() != null, BizLiquorRecords::getUnitPrice, bo.getMinPrice());
        lqw.le(bo.getMaxPrice() != null, BizLiquorRecords::getUnitPrice, bo.getMaxPrice());
        lqw.orderByDesc(BizLiquorRecords::getPurchaseDate);
        return lqw;
    }

    /**
     * 新增白酒记录
     *
     * @param bo 白酒记录
     * @return 是否新增成功
     */
    public Boolean insertByBo(BizLiquorRecordsBo bo) {
        BizLiquorRecords add = MapstructUtils.convert(bo, BizLiquorRecords.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改白酒记录
     *
     * @param bo 白酒记录
     * @return 是否修改成功
     */
    public Boolean updateByBo(BizLiquorRecordsBo bo) {
        BizLiquorRecords update = MapstructUtils.convert(bo, BizLiquorRecords.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BizLiquorRecords entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除白酒记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
